package ebusiness.ebusiness.Controllers;

import ebusiness.ebusiness.Dao.TorlleyDao.TorlleyQueryVO;
import ebusiness.ebusiness.Dao.UserDao.UserQueryVO;
import ebusiness.ebusiness.Entity.Address;
import ebusiness.ebusiness.Entity.Good;
import ebusiness.ebusiness.Entity.UserEntity.RegisteUser;
import ebusiness.ebusiness.Entity.UserEntity.User;
import ebusiness.ebusiness.Entity.UserEntity.Visitor;
import ebusiness.ebusiness.Mapper.JsonUtil.StatusUtil;
import ebusiness.ebusiness.Mapper.PathUtil.PagePath;
import ebusiness.ebusiness.Mapper.PathUtil.WebPath;
import ebusiness.ebusiness.Models.Front.ShoppingTorlley.CartView;
import ebusiness.ebusiness.Models.Front.ShoppingTorlley.TorlleyImp;
import  ebusiness.ebusiness.Mapper.JsonUtil.strTOjson;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

@RestController
public class CarController {

    @RequestMapping(path = WebPath.ADDCART,method = RequestMethod.POST)
    public String addCART( HttpSession session,int id,int num) {
        TorlleyImp tp=null;
        Object oj=session.getAttribute("user");
        int uid=-1;
        if(oj!=null){
            uid=(int)oj;
        }
        if(session.getAttribute("cart")==null){


            
            if(uid <=0)
            tp= TorlleyImp.NewInstance();
            else {
                try {

                    tp= TorlleyImp.HistoryCart(uid);
                    tp.SetUser(uid);
                }catch (Exception e){
                    tp = TorlleyImp.NewInstance(uid);
                    tp.SetUser(uid);
                    return StatusUtil.EMPTY;
                }

            }
        }else {
            tp=(TorlleyImp) session.getAttribute("cart");
        }
        int result=1;
        TorlleyQueryVO torlleyQueryVO=new TorlleyQueryVO();

        //添加购物车商品
        // 进数据库
        try {

            if(tp.cotainGood(id)){
                result =tp.addGood(id,num);
                if(uid>0)
                torlleyQueryVO.modifyTemplory(tp.getTorlley(id));


            }else {
                result=tp.addGood(id,num);
                if(uid>0)
                torlleyQueryVO.addATemplory(tp.getTorlley(id)) ;
            }

        }catch (Exception e){
            return StatusUtil.Fail;
        }

        Good gd=tp.getAGood(id);

        session.setAttribute("cart",tp);

        return StatusUtil.Success;
    }

    @RequestMapping(path = WebPath.CARDNUM,method = RequestMethod.POST)
    public String ModifyNUM(HttpSession session,int goodid,int num) {
        TorlleyImp tp=null;
        if(session.getAttribute("cart")==null){
            //error handel
            return StatusUtil.EMPTY;
        }else {
            tp=(TorlleyImp) session.getAttribute("cart");
        }
        int rs=-1;
        try {
            tp.modifyGoodNum(goodid,num);
            TorlleyQueryVO torlleyQueryVO=new TorlleyQueryVO();
            torlleyQueryVO.modifyTemplory(tp.getTorlley(goodid));
        }catch (Exception e){

            return StatusUtil.Fail;
        }



        return StatusUtil.Success;
    }
    @RequestMapping(path = WebPath.CARDELETE,method = RequestMethod.POST)
    public String CardDelete(HttpSession session,String id) {
    	System.out.println(id);
    	int goodid;
    	goodid=Integer.parseInt(id);
        TorlleyImp tp=null;
        if(session.getAttribute("cart")==null){
            //error handel
           System.out.println("1234");
           return StatusUtil.EMPTY;
        }else {
            tp=(TorlleyImp) session.getAttribute("cart");
        }
        try {
            tp.removeGood(goodid);
            Object oj= session.getAttribute("user");
            int user=-1;
            if(oj!=null)
            user=(int)oj;
            if(user>0){
                TorlleyQueryVO torlleyQueryVO=new TorlleyQueryVO();
                torlleyQueryVO.deleteTemplory(goodid,user);
            }
        }catch (Exception e){
            e.printStackTrace();
            return StatusUtil.EMPTY;
        }



        return StatusUtil.Success;
    }
    /*@RequestMapping(path = WebPath.AddADRESS,method = RequestMethod.POST)
    public String AddAdress( HttpSession session,String name,String address_province,String address_city,String address,String address_postal){
            int user=(int)session.getAttribute("user");
        String value="-1";


            if(user>0){

                try {
                    Address myaddress=new Address();
                    UserQueryVO userQueryVO=new UserQueryVO();

                    myaddress.setAddress(address);
                    myaddress.setAddress_city(address_city);
                    myaddress.setAddress_postal(address_postal);
                    myaddress.setCustomer_name(name);
                    myaddress.setAddress_province(address_province);

                    myaddress.setCustomer_id(user);
                    myaddress.setAddress_id(userQueryVO.GeneralAddressID());

                    userQueryVO.addAddress(myaddress);
                    return StatusUtil.Success;
                }catch (Exception e){

                    return StatusUtil.Fail;
                }


            }



        return StatusUtil.Fail;
    }*/







}
